package com.just521.xinshoutong.database;
import java.io.File;
import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;

@SuppressWarnings("unused")
interface SDCardStatusChangedCtrl{
	public void ChangedAvailable();
	public void ChangedUnAvailable();
}

public class SDCardAccessor extends BroadcastReceiver {  
	private enum SDCardStatusChanged{
		SDCardStatusChanged_Available,
		SDCardStatusChanged_UnAvailable
	}
    private static boolean	m_sdcardAvailable 				=	false;  
    private static boolean	m_sdcardAvailabilityDetected	=	false;  
    public static String 	m_sdcardDir						=	null;
    public static final String 					m_strTXTPath			=	"/TXT";
    public static final String 					m_strTouchFilePath		=	"/touch";
    public static final String 					m_strAudioFilePath		=	"/record";
    public static final String 					m_strDatabaseFilePath	=	"/database";
    public static final String 					m_strimagecacheFilePath	=	"/imagecache";
    public static final String 					m_strBackupFilePath		=	"/backup";
    public static final String 					m_strAppFilePath		=	"/xinshoutong";
    private static 		File					m_txtDataDir 			=	null;
    private static 		File					m_audioDataDir 			=	null;
    private static 		File					m_touchDataDir 			=	null;
    private static 		File					m_mediaDataDir 			=	null;
    private static		File					m_DownLoadFilesDir		=	null;
    
    private static 		ArrayList<SDCardStatusChangedCtrl>	m_ctrlList	=	null;
    private static 		String 								m_strBackupFolderName = null;
    public	static final String								m_strNoteDataBase = "note.db";
    
    
	
    private static Context m_context = null;
//    public static synchronized boolean detectSDCardAvailability() {  
//        boolean result = false;  
//        try {  
// 
//        } catch (Exception e) {  
//            // Can't create file, SD Card is not available  
//            e.printStackTrace();  
//        } finally {  
//            sdcardAvailabilityDetected = true;  
//            sdcardAvailable = result;  
//        }  
//        return result;  
//    }  
    public static boolean isSDCardAvailable() {  
        if(!m_sdcardAvailabilityDetected) {  
        	m_sdcardAvailable = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
        	m_sdcardAvailabilityDetected = true;  
        	if(m_sdcardAvailable ){
        		if(m_sdcardDir==null){
        			m_sdcardDir = Environment.getExternalStorageDirectory().toString();	
        		}
        		notifyCtrl(SDCardStatusChanged.SDCardStatusChanged_Available);
        	}
        }  
        return  m_sdcardAvailable;  
    }  

    public static String getNoteDataBaseFullPath(){
     	String retFullPath = "";
     	if(isSDCardAvailable()){
     		retFullPath = m_sdcardDir + m_strAppFilePath + m_strDatabaseFilePath;
     		File fp = new File(retFullPath);
     		if(!fp.exists()){
     			fp.mkdirs();
     		}
     		retFullPath+="/"+m_strNoteDataBase;
     	}else{
     		
     	}
     	return retFullPath;
    } 
    public static String getImageCachePath(){
     	String retFullPath = "";
     	if(isSDCardAvailable()){
     		retFullPath = m_sdcardDir + m_strAppFilePath + m_strimagecacheFilePath;
     		File fp = new File(retFullPath);
     		if(!fp.exists()){
     			fp.mkdirs();
     		}
     	}else{
     		
     	}
     	return retFullPath;
    } 
    public static void advise(SDCardStatusChangedCtrl ctrl, Context context){
    	m_context = context.getApplicationContext();
    	if(m_ctrlList == null){
    		m_ctrlList = new ArrayList<SDCardStatusChangedCtrl>();
    	}
    	m_ctrlList.add(ctrl);
    	
    }
    public static void unadvise(SDCardStatusChangedCtrl ctrl){
    	if(m_ctrlList!=null){
    		m_ctrlList.remove(ctrl);
    	}
    }
    public static void deleteFile(File fileTemp){ 
    	if(fileTemp==null){
    		return;
    	}
		if(fileTemp.exists()){ 
			if(fileTemp.isFile()){ 
				fileTemp.delete(); 
			}else if(fileTemp.isDirectory()){ 
				File files[] = fileTemp.listFiles(); 
				if(files!=null){
					for(int i=0;i<files.length;i++){ 
						deleteFile(files[i]); 
					} 
				}
			} 
			fileTemp.delete(); 
		}else{ 
		}	
    } 
    public void onReceive(Context context, Intent intent) {  
        m_sdcardAvailabilityDetected = false; 
        if(intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)){
        	m_sdcardAvailable = true;
        	if(m_sdcardDir==null){
        		m_sdcardDir = Environment.getExternalStorageDirectory().toString();
        	}
        	notifyCtrl(SDCardStatusChanged.SDCardStatusChanged_Available);
        }else{
        	android.os.Process.killProcess(android.os.Process.myPid());
        	notifyCtrl(SDCardStatusChanged.SDCardStatusChanged_UnAvailable);
        	m_sdcardAvailable = false;
        }
        m_sdcardAvailabilityDetected = true;  
    }  
    private static void notifyCtrl(SDCardStatusChanged status){
    	if(m_ctrlList!=null){
    		int size = m_ctrlList.size();
        	for(int i = 0; i < size; i++){
        		SDCardStatusChangedCtrl temp = m_ctrlList.get(i);
        		if(temp!=null){
        			if(status == SDCardStatusChanged.SDCardStatusChanged_Available){
        				temp.ChangedAvailable();
        			}else{
        				temp.ChangedUnAvailable();	
        			}
        		}
        	}	
    	}
    }
}
